package cn.rkylin.oms.refund.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.refund.adapter.RefundAdapter;
import cn.rkylin.oms.refund.adapter.RefundAdapterFactory;

/**
 * 自动下载指定店铺的退款单
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/refundManager")
public class RefundController extends ApolloController {

    /**
     * 日志类
     */
    @Autowired
    private LogUtil logUtil;

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final Log logger = LogFactory.getLog(RefundController.class);

    /**
     * 下载退款单
     * localhost:8085/refundManager/getRefundAll.action?shopId=ad4d1392e16511e7910200163e035834&start=2017-11-11&end=2017-12-13
     *
     * @param shopId
     * @param start
     * @param end
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getRefundAll", method = RequestMethod.GET)
    public Map getRefundAll(String shopId, String start, String end) throws Exception {
        logUtil.info(new DBLog(shopId, "自动下载平台退款单Controller开始",
                "自动下载平台退款单", "shopId="+shopId));

        if (StringUtils.isEmpty(start) && StringUtils.isNotEmpty(end)) {
            Calendar c = Calendar.getInstance();
            c.setTime(simpleDateFormat.parse(end));
            c.add(Calendar.MONTH, -3);    //得到前一个月
            // 取过去1个月的商品
            start = simpleDateFormat.format(c.getTime());
        }
        start = URLDecoder.decode(start, "UTF-8");
        end = URLDecoder.decode(end, "UTF-8");

        // 返回值
        Map<String, Object> resultMap = new HashMap<String, Object>();

        RefundAdapter refundAdapter = RefundAdapterFactory.getRefundAdapter(shopId);
        if (refundAdapter == null) {
            resultMap.put("result", "adapter is null");
            resultMap.put("success", "false");
            return resultMap;
        }

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("startTime", start);
        paramMap.put("endTime", end);

        int result = 0;
        // 退款单下载 分单
        try {
            result = refundAdapter.download(shopId,paramMap);
        } catch (Exception ex) {
            logUtil.error(new DBLog(shopId, "自动下载平台退款单Controller结束",
                    "自动下载平台退款单", "失败:"+ex.getMessage()+";shopId="+shopId),ex);
        }


        if (result == 0) {
            resultMap.put("success", "false");// easy schedule
        } else {
            resultMap.put("success", "true");
        }

        return resultMap;

    }


    /**
     * 按单号下载退款单
     * localhost:8082/refundManager/getRefund.action?shopId=ad4d1392e16511e7910200163e035834&refundId=6330853701690486
     * localhost:8082/refundManager/getRefund.action?shopId=ad4d1392e16511e7910200163e035834&buyerNick=hengkeng110
     * @param shopId
     * @param refundId
     * @param buyerNick
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getRefund", method = RequestMethod.GET)
    public Map getRefund(String shopId, String refundId, String buyerNick) throws Exception {
        // 返回值
        Map<String, Object> resultMap = new HashMap<String, Object>();

        RefundAdapter refundAdapter = RefundAdapterFactory.getRefundAdapter(shopId);
        if (refundAdapter == null) {
            resultMap.put("result", "adapter is null");
            resultMap.put("success", "false");
            return resultMap;
        }

        Map<String, Object> paramMap = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(refundId)) {
            refundId = URLDecoder.decode(refundId, "UTF-8");
            paramMap.put("refundId", refundId);
        }else if(StringUtils.isNotEmpty(buyerNick)){
            buyerNick = URLDecoder.decode(buyerNick, "UTF-8");
            paramMap.put("buyerNick", buyerNick);
        }

        int result = 0;
        // 退款单下载 分单
        try {
            result = refundAdapter.download(shopId,paramMap);
        } catch (Exception ex) {
            logUtil.error(new DBLog(shopId, "手动下载平台退款单",
                    "手动下载平台退款单", "失败:"+ex.getMessage()+";shopId="+shopId +";refundId="+refundId+";buyerNick="+buyerNick),ex);
        }


        if (result == 0) {
            resultMap.put("success", "false");// easy schedule
        } else {
            resultMap.put("success", "true");
        }

        return resultMap;

    }
}
